package com.cloud.school.bus.teacherhelper.modules.notice.adapter;

import java.util.ArrayList;
import java.util.List;

import com.android.support.jhf.universalimageloader.core.ImageLoader;
import com.android.support.jhf.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.android.support.jhf.utils.DateUtils;
import com.android.support.jhf.utils.ImageUtil;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.TeacherHelperApplication;
import com.cloud.school.bus.teacherhelper.entitys.Notice;
import com.cloud.school.bus.teacherhelper.entitys.Picture;
import com.cloud.school.bus.teacherhelper.modules.home.BigPictureActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NoticeAdapter extends BaseAdapter {

	private Context mContext;
	private List<Notice> mNotices;
	private TeacherHelperApplication mApplication;

	public NoticeAdapter(Context context, List<Notice> notices) {
		mContext = context;
		mNotices = notices;
		mApplication = (TeacherHelperApplication) context
				.getApplicationContext();
	}

	@Override
	public int getCount() {
		return mNotices.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mNotices.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {

		if (null == arg1) {
			arg1 = LayoutInflater.from(mContext).inflate(
					R.layout.adapter_notice, null);
		}

		final Notice notice = mNotices.get(arg0);

		ImageView icLeftImageView = (ImageView) arg1
				.findViewById(R.id.icLeftImageView);
		if ("0".equals(notice.isconfirm) || "".equals(notice.isconfirm)) {
			// 不需要回执,显示信封icon
			icLeftImageView.setImageResource(R.drawable.ic_xinfeng);
		} else if ("1".equals(notice.isconfirm)) {
			// 需要回执，显示星星icon
			icLeftImageView.setImageResource(R.drawable.ic_notice_xingxing);
		}

		TextView titleTextView = (TextView) arg1
				.findViewById(R.id.titleTextView);
		if (TextUtils.isEmpty(notice.noticetitle)) {
			titleTextView.setVisibility(View.GONE);
		} else {
			titleTextView.setVisibility(View.VISIBLE);
			titleTextView.setText(notice.noticetitle);
		}

		TextView contentTextView = (TextView) arg1
				.findViewById(R.id.contentTextView);
		if (TextUtils.isEmpty(notice.noticecontent)) {
			contentTextView.setVisibility(View.GONE);
		} else {
			contentTextView.setVisibility(View.VISIBLE);
			contentTextView.setText(notice.noticecontent);
		}

		ImageView imageView = (ImageView) arg1.findViewById(R.id.imageView);
		if (null == notice.plist || 0 == notice.plist.size()) {
			imageView.setVisibility(View.GONE);
		} else {
			imageView.setVisibility(View.VISIBLE);
			ImageLoader.getInstance().displayImage(notice.plist.get(0).source,
					imageView, mApplication.mCacheDisplayImageOptions,
					new SimpleImageLoadingListener() {
						@Override
						public void onLoadingComplete(String imageUri,
								View view, Bitmap loadedImage) {
							ImageView tempImageView = (ImageView) view;
//							tempImageView.setImageBitmap(loadedImage);
							if(imageUri.startsWith("http://")){
								tempImageView.setImageBitmap(loadedImage);
							}else if(imageUri.startsWith("file:///")){
								ImageUtil.setRotaingImageBitmap(imageUri.replace("file:///", "/"), loadedImage, tempImageView);
							}else {
								ImageUtil.setRotaingImageBitmap(imageUri, loadedImage, tempImageView);
							}
						}
					});
		}
		imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				List<Picture> pictureList = new ArrayList<Picture>();
				Picture picture = new Picture();
				picture.setPicturePath(notice.plist.get(0).source);
				pictureList.add(picture);
				Intent intent = new Intent(mContext, BigPictureActivity.class);
				intent.putExtra("pictureList", (ArrayList<Picture>) pictureList);
				mContext.startActivity(intent);
			}
		});

		TextView dateTextView = (TextView) arg1.findViewById(R.id.dateTextView);
		long addtime = 0L;
		try {
			addtime = Long.parseLong(notice.addtime);
		} catch (Exception e) {
		}
		if (0 == addtime) {
			dateTextView.setVisibility(View.INVISIBLE);
		} else {
			dateTextView.setVisibility(View.VISIBLE);
			dateTextView.setText(DateUtils.dateFormat(addtime * 1000,
					"MM-dd HH:ss"));
		}

		TextView stateTextView = (TextView) arg1
				.findViewById(R.id.stateTextView);
		if ("1".equals(notice.isconfirm)) {
			stateTextView.setVisibility(View.VISIBLE);
			stateTextView.setText(mContext.getString(R.string.confirmed_x)
					+ " "
					+ (null == notice.sisconfirm ? "0"
							: notice.sisconfirm.length));
		} else if ("0".equals(notice.isconfirm)) {
			stateTextView.setVisibility(View.GONE);
		} else {
			stateTextView.setVisibility(View.GONE);
		}

		return arg1;
	}

}
